package jsyf.oa.dao.system;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.oa.entity.system.User2RegionEntity;
import jsyf.oa.idao.BaseDao;
import jsyf.oa.ientity.IBaseEntity;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author: 徐波
 * @creat: 2017/11/23 0023 9:59
 * description:
 */
@Repository
public class User2RegionDao extends BaseDao{
    public User2RegionDao() {
        this.iBaseEntity = new User2RegionEntity();
    }

    @Override
    public IBaseEntity convertJson2Entity(JSONObject json) {
        return  JSON.toJavaObject(json, User2RegionEntity.class);
    }

    public JSONObject getRegionByUserId(String userId){
        StringBuffer sb = new StringBuffer();
        sb.append(" SELECT\n");
        sb.append(" v_sys_user.id FROM t_sys_region \n");
        sb.append(" LEFT JOIN t_sys_user_2_region ON t_sys_region.ID=t_sys_user_2_region.REGION_ID\n");
        sb.append(" LEFT JOIN v_sys_user ON t_sys_user_2_region.USER_ID=v_sys_user.ID\n");
        sb.append(" WHERE t_sys_region.ID IN(\n");
        sb.append(" SELECT REGION_ID FROM t_sys_user_2_region \n");
        sb.append(" WHERE USER_ID="+"'"+userId+"')\n");
        sb.append(" AND t_sys_region.PARENT_ID=0\n");
        sb.append(" AND v_sys_user.`roleName`='大区经理'\n");
        sb.append(" AND v_sys_user.jobState!=1;");
        JSONObject jsonObject = queryForJsonObject(sb.toString());
        return jsonObject == null ? null : jsonObject;
    }

    public List<JSONObject> getBigManageByRegionId(String regionId) {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT\n" +
                "\tt_sys_user_2_region.USER_ID AS userId\n" +
                "FROM\n" +
                "\tt_sys_user_2_region\n" +
                "LEFT JOIN v_sys_user ON t_sys_user_2_region.USER_ID = v_sys_user.ID\n" +
                "WHERE\n" +
                "\tREGION_ID = '"+regionId+"'\n" +
                "AND v_sys_user.roleName = '大区经理';");
        return super.queryForJsonList(sql.toString());
    }
    public List<JSONObject> getRegionUserByUserId(String userId) {
        StringBuffer sb = new StringBuffer();
        sb.append(" select USER_ID as userId FROM t_sys_user_2_region WHERE REGION_ID in( \n ");
        sb.append(" SELECT REGION_ID from t_sys_user_2_region where USER_ID="+"'"+userId+"')\n");
        List<JSONObject> jsonList = super.queryForJsonList(sb.toString());
        return jsonList;
    }
}
